package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.Warehouse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.Safetyinventroy;
import cn.b.sky.mes.mm.model.condition.SafetyinventroyCondition;
import cn.b.sky.mes.mm.service.SafetyinventroyService;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : SafetyinventroyController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>安全库存控制器<br> 
 * <strong>Create on : 2016-3-1 下午4:47:24<br></strong>
 * <p> 
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人    |    修改日期    |    修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/safetyinventroy/*")
public class SafetyinventroyController extends SecurityController {
  @Resource
   SafetyinventroyService safetyinventroyService;

  @Resource
  private WarehouseService warehouseService;

  @RequestMapping("safetyinventroyList")
  public String safetyinventroyList(@ModelAttribute("condition") SafetyinventroyCondition condition, String navTabId,
      Model model) {
    condition.setSiteCode(this.getSiteCode());
    List<Safetyinventroy> safetyinventroys = safetyinventroyService.getSafetyinventroysByCondition(condition);
    int count = safetyinventroyService.getSafetyinventroyCountByCondition(condition);
    condition.setData(safetyinventroys);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute("navTabId", navTabId);
    return "mm/safetyinventroy/safetyinventroy_list";
  }

  @RequestMapping("addSafetyinventroy")
  public String addSafetyinventroy(String navTabId, Model model) {

    List<Warehouse> warehouses = warehouseService.selectWarehouseListBySiteCode(this.getSiteCode());
    model.addAttribute("warehouses", warehouses);
    model.addAttribute("navTabId", navTabId);
    return "mm/safetyinventroy/safetyinventroy_add";
  }

  @RequestMapping("modifySafetyinventroy")
  public String modifySafetyinventroy(Long id, String navTabId, Model model) {
    if (null == id) {
      throw ImeException.newException("parameterNull.M");
    }
    Safetyinventroy safetyinventroy = safetyinventroyService.getSafetyinventroyByPk(this.getSiteCode(), id);
    model.addAttribute("safetyinventroy", safetyinventroy);
    model.addAttribute("navTabId", navTabId);
    return "mm/safetyinventroy/safetyinventroy_edit";
  }

  @RequestMapping("viewSafetyinventroy")
  public String viewSafetyinventroy(Long id, String navTabId, Model model) {
    if (null == id) {
      throw ImeException.newException("parameterNull.M");
    }
    Safetyinventroy safetyinventroy = safetyinventroyService.getSafetyinventroyByPk(this.getSiteCode(), id);
    model.addAttribute("safetyinventroy", safetyinventroy);
    model.addAttribute("navTabId", navTabId);
    return "mm/safetyinventroy/safetyinventroy_view";
  }

  @RequestMapping("saveSafetyinventroy")
  @ResponseBody
  public DWZResult saveSafetyinventroy(Safetyinventroy safetyinventroy, Model model, String navTabId) {
    if (safetyinventroy == null || safetyinventroy.isNullObject()) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }
    safetyinventroy.setSiteCode(this.getSiteCode());
    this.setCreateInfo(safetyinventroy);
    if (safetyinventroy.getId() == null) {
      if (!safetyinventroyService.isSetStorageLocationAndWarehouseCode(safetyinventroy)) {
        return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "materialCodeIsSameInventroy.M"));
      }
      safetyinventroyService.saveSafetyinventroy(safetyinventroy);
    } else {
      safetyinventroyService.modifySafetyinventroy(safetyinventroy);
    }
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("deleteSafetyinventroy")
  @ResponseBody
  public DWZResult deleteSafetyinventroy(String ids, String navTabId, Model model, HttpServletRequest request) {
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] idArr = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String message = safetyinventroyService.deleteSafetyinventroyBySiteCodeAndId(this.getSiteCode(),
          Long.valueOf(idArr[i]));
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        BatchJobReport report = new BatchJobReport(idArr[i], this.getImeViewMessageI18n(this.getCurrentLanguage(),
            message), false, this.getCurrentLanguage(), this.getSiteCode());
        reports.add(report);
      } else {
        BatchJobReport report = new BatchJobReport(idArr[i], "", true, this.getCurrentLanguage(), this.getSiteCode());
        reports.add(report);
      }
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }
}
